package com.tc.start_developmen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class StartDevelopmentEnvironment {
    public static void main(String[] paramArrayOfString)
            throws Exception {
        String path = StartDevelopmentEnvironment.class.getResource("").getFile();
        path = path.substring(1, path.length());
        List lines = Files.readAllLines(Paths.get(path + "config.txt", new String[0]), Charset.forName("UTF-8"));
        if (lines != null) {
            for (int i = 0; i < lines.size(); i++) {
                String line = (String) lines.get(i);
                if ((line != null) &&
                        (!"".equals(line.trim()))) {
                    if (line.startsWith("open")) {
                        open(line.substring(4, line.length()), path);
                    } else if ((line.startsWith("close")) &&(line.contains("exe")) &&(line.length() >= 5)) {
                        line = line.substring(5, line.length());
                        String str3 = line.substring(0, line.indexOf("exe") + 3).trim();
                        if (!line.trim().endsWith("exe")) {
                           closep(str3,str3.substring(str3.lastIndexOf("\\") + 1, str3.length()));
                        }
                    }
                }
            }
        }
    }

    public static void open(String command, String currentPath)
            throws Exception {
        File temp = null;
        String fileName = null;
        String openPath = null;
        FileOutputStream tempFileOutputStream = null;
        String disc =null;//盘符
        try {
            if ((command.endsWith(".bat")) || (command.endsWith(".exe"))) {//文件方式运行
                command = command.trim();
                fileName = command.substring(command.lastIndexOf("\\") + 1, command.length());
                temp = new File(currentPath + fileName + "temp.bat");
                tempFileOutputStream = new FileOutputStream(temp);
                disc = command.substring(0, command.indexOf(":") + 1);
                tempFileOutputStream.write(((String) disc + "\r\n").getBytes());
                openPath = command.substring(0, command.lastIndexOf("\\"));
                tempFileOutputStream.write(("cd " + openPath + "\r\n").getBytes());
                tempFileOutputStream.write(fileName.getBytes());
            }
            if (command.contains(",")) { //命令形式运行
                String commands [] = command.split(",");
                if (commands.length == 2) {
                    openPath = commands[0];
                    command =  commands[1];
                    fileName = command.replaceAll(" ", "_");
                    disc = openPath.substring(0, openPath.indexOf(":") + 1);
                    temp = new File(currentPath + fileName + "temp.bat");
                    tempFileOutputStream = new FileOutputStream(temp);
                    tempFileOutputStream.write((disc + "\r\n").getBytes());
                    tempFileOutputStream.write(("cd " + openPath + "\r\n").getBytes());
                    tempFileOutputStream.write((command + "\r\n").getBytes());
                }
            }
            Runtime localRuntime = Runtime.getRuntime();
            localRuntime.exec("cmd /c start " + currentPath + fileName + "temp.bat");
            temp.delete();
            temp = null;
        } catch (Exception e) {
            throw e;
        } finally {
            if (tempFileOutputStream != null) {
                try {
                    tempFileOutputStream.close();
                } catch (IOException localIOException2) {
                    localIOException2.printStackTrace();
                }
            }
            if (temp != null) {
                temp.delete();
            }
        }
    }

    public static void closep(String path, String name)
            throws Exception {
        InputStream localInputStream = null;
        InputStreamReader localInputStreamReader = null;
        BufferedReader localBufferedReader = null;
        String line = null;
        try {
            name = name == null ? "java.exe" : name;
            Process localProcess = Runtime.getRuntime().exec("wmic process where name=\"" + name + "\" list full");
            localInputStream = localProcess.getInputStream();
            localInputStreamReader = new InputStreamReader(localInputStream);
            localBufferedReader = new BufferedReader(localInputStreamReader);
            boolean bool = false;
            while ((line = localBufferedReader.readLine()) != null) {
                if ((line != null) && (!"".equals(line.trim())) &&(line.contains("="))) {
                    String key = line.substring(0, line.indexOf("="));
                    String value = line.substring(line.indexOf("=") + 1, line.length());
                    if ((key != null) && (!"".equals(key.trim())) &&
                            (value != null) && (!"".equals(value.trim()))) {
                        if ("CommandLine".equals(key)) {
                            bool = path == null ? true : value.endsWith(path);
                        }
                        if ((bool) && ("ProcessId".equals(key))) {
                            Runtime.getRuntime().exec("wmic process where processid=\"" + value + "\" delete");
                            bool = false;
                        }
                    }
                }
            }
        } catch (IOException localIOException) {
            throw new Exception("get pid error");
        } finally {
            if (localBufferedReader != null) {
                localBufferedReader.close();
            }
            if (localInputStreamReader != null) {
                localInputStreamReader.close();
            }
            if (localInputStream != null) {
                localInputStream.close();
            }
        }
    }
}
